﻿Ext.define('app.prm.project.ProjectController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.project',
    init: function () {
        var me = this;
    },
    onAdd: function () {
        var grid = this.getView();
        var win = Ext.create("app.prm.project.Edit", { title: '项目创建' });
        win.on("savesuccess", function (obj) {
            win.close();
            grid.store.load();
        });
        win.show();
    },
    onApply: function () {
        this.redirectTo('app.prm.project.PrmApply');
    },
    onCellClick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var t = e.position.column.dataIndex;
        if (t == 'projectNo') {
            Ext.util.Cookies.set('projectinfo' + record.data.projectId, record.data.projectName + "(" + record.data.projectNo + ")");

            this.redirectTo('app.prm.detail.Index/' + record.data.projectId);
        }
        else if (t == 'productNo') {
            if (record.data.productId != 0) {

                Ext.util.Cookies.set('productinfo' + record.data.productId, record.data.productNo);
                this.redirectTo('app.prm.product.Detail/' + record.data.productId);
            }
        }
    },
    onDocCelldblclick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var column = e.position.column;
        var value = record.data[column.dataIndex];
        console.log(value);

        var DocTypeId = record.data.DocTypeId;
        var projectId = 0;
        var projectName = '';
        var projectNo = '';
        var docTypeId = 0;

        if (DocTypeId >= 0) {
            projectId = column.projectId;
            projectName = column.text;
            projectNo = column.projectNo;
            docTypeId = DocTypeId;
        }
        else {
            projectId = record.data.projectId;
            projectName = record.data.projectName;
            projectNo = record.data.projectNo;
            docTypeId = column.docType;
        }

        if (projectId == undefined)
        {
            return;
        }

        if ((value == undefined || value.indexOf('/') == -1)&&projectId>0) {
            
            Ext.util.Cookies.set('projectinfo' + projectId, projectName + "(" + projectNo + ")");

            this.redirectTo('app.prm.detail.Index/' + projectId);
            return;
        }
        var v = value.split('/')[0];
        console.log(v);
        if (v == undefined||v == ''||v == 0) {
            return false;
        }

       
        Ext.util.Cookies.set('projectdocument' + projectId, projectName + "(" + projectNo + ")文档");
        Ext.util.Cookies.set('projectdocumentreport' + projectId, docTypeId);
        this.redirectTo('app.prm.document.Index/' + projectId);
    },
    onEditProject: function (grid, rowIndex, colIndex) {
        var record = grid.store.getAt(rowIndex);

        Ext.util.Cookies.set('projectinfo' + record.data.id, record.data.projectName + "(" + record.data.projectNo + ")");

        this.redirectTo('app.prm.detail.Index/' + record.data.id);
    },
    onDbClick: function (grid, record, tr, rowIndex, e, ept) {
        Ext.util.Cookies.set('projectinfo' + record.data.id, record.data.projectName + "(" + record.data.projectNo + ")");

        this.redirectTo('app.prm.detail.Index/' + record.data.id);
    },
    onYearPhaseProjectDbClick: function (grid, record, tr, rowIndex, e, ept) {
        //--年度-项目阶段情况数据；
        Ext.util.Cookies.set('projectinfo' + record.data.id, record.data.projectName + "(" + record.data.projectNo + ")");
        this.redirectTo('app.prm.detail.Index/' + record.data.id);
        this.getView().close();
    },
    onInit: function (view) {
        App.AjaxPost('/api/services/app/permission/HasAddProject', {}, function (data) {
            view.getViewModel().set('hasAdd', !data.result);
        });
        this.onStartSearch();
    },
    onStartSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("projectsearch");
        if (panel != null)
        {
            grid.store.filter = panel.form.getForm().getValues();
        }
        if (grid.pagingBar == undefined)
        { grid.store.reload(); }
        else
        { grid.pagingBar.moveFirst(); }
        
    },
    onClearSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("projectsearch");
        panel.form.getForm().reset();
        this.onStartSearch();
    }
    ,
    onFollowCustomerClick: function (view, record, item, index, e, eOpts) {

        var view = this.getView();
        var customerName = record.get('name');
        view.renderHistoryWin(customerName); 

        var view = this.getView();
        var tab = view.getTabPanel(),
        form1 = view.getProjectSearchPanel(),
        form2 = view.getInquirySearchPanel();

        form1.getForm().setValues({ customer: record.get('name') });
        form2.getForm().setValues({ CustomerName: record.get('name') });
        if (tab.getActiveTab().title.indexOf('项目') > 0) {
            this.onFollowProjectStartSearch();
        }
        else {
            this.onFollowInquiryStartSearch();
        }
    },
    onFollowProjectStartSearch: function () {
        console.log('载入项目');
        var view = this.getView(),
        grid = view.getProjectGrid(),
        form = view.getProjectSearchPanel();

        grid.store.filter = form.getForm().getValues();
        grid.store.load();
    }
    , onFollowProjectAllSearch: function () {
        var view = this.getView(),
        form = view.getProjectSearchPanel();
        form.getForm().setValues({ status: [10, 20, 30, 40, 50] });
        this.onFollowProjectStartSearch();
    }
    , onFollowProjectDoingSearch: function () {
        var view = this.getView(),
        form = view.getProjectSearchPanel();
        form.getForm().setValues({ status: [10, 30, 50] });
        this.onFollowProjectStartSearch();
    },
    onFollowInquiryStartSearch: function () {
        console.log('载入询盘');
        var view = this.getView(),
        grid = view.getInquiryGrid(),
        form = view.getInquirySearchPanel();

        grid.store.filter = form.getForm().getValues();
        grid.store.load();
    }
});
